<?php

namespace Platform\Controller;

use Platform\Event\UserEvent;

/**
 * @apiDefine ApiErrorExample
 *
 * @apiErrorExample 失败返回
 * HTTP/1.1 200 OK
 * {
 *  "code": 100,
 *  "info": "",
 *  "data": "",
 * }
 *
 */

/**
 * @apiDefine ApiSuccessExample
 *
 * @apiSuccessExample 成功返回
 * HTTP/1.1 200 OK
 * {
 *  "data": "",
 *  "info": "",
 *  "code": 200,
 * }
 *
 */

class UserController extends BaseController {

	/**
	 * @api {post}  /platform/user/login 用户登录授权
	 * @apiVersion 1.0.0
	 * @apiName login
	 * @apiGroup 用户
	 *
	 * @apiParam {string} app_id 平台的应用ID
	 * @apiParam {string} id 平台的用户ID 作为唯一标识
	 * @apiParam {string} nickname 平台的用户昵称
	 * @apiParam {string} avatar 平台的用户头像
	 * @apiParam {string} sign 签名<参考`签名方式`>
	 *
	 * @apiSuccess {String} info  提示信息
	 * @apiSuccess {Number} code  错误码,200代表正常
	 * @apiSuccess {Object} data  数据
	 * @apiSuccess {Number} data.appToken  授权码
	 *
	 * @apiUse ApiSuccessExample
	 *
	 * @apiUse ApiErrorExample
	 */
	public function login() {
		$data = I();
		if (empty($data['app_id']) ||
			empty($data['id']) ||
			empty($data['nickname']) ||
			empty($data['avatar']) ||
			empty($data['sign'])
		) {
			$this->errParam();
		}

		$signParam = [
			'app_id' => $data['app_id'],
			'id' => $data['id'],
			'nickname' => $data['nickname'],
			'avatar' => $data['avatar'],
		];

		#TODO 上线开启
		$this->checkSign($signParam);

		$token = UserEvent::login($this->app, $data);

		$res['appToken'] = $token;
		$this->resOK($res);
	}

}